<?php
namespace Admin\Controller;

class ErrorLogController extends \Admin\Controller\AdminController
{
	/**
	 * 错误日志列表
	 * @author 尹雷
	 */
	public function logList()
	{
		$con = I('condition');
		$where['status'] = array('egt', 0);
	
		//查询条件
		if($con) {
			if(preg_match('/^[1-9]\d*$/', $con)) {
				$where['id'] = $con;
			} else {
				$where['api_code'] = array('like', '%'.$con.'%');
			}
		}
	
		//获取错误日志信息
		$list = $this->lists('ErrorLog', $where);
	
		if(!empty($list)) {
			int_to_string($list, array('status'=>array(1=>'已处理',-1=>'删除',0=>'禁用',2=>'未处理')));
		}
		
		//die(json_encode($list));
		$this->assign('_title', '错误日志列表');
		$this->assign('_list', $list);
		$this->display('list');
	}
	
	/**
	 * 更新错误日志状态
	 * @author 尹雷
	 */
	public function changeStatus($method=null)
	{
		$id = array_unique((array)I('id',0));
		$id = is_array($id) ? implode(',',$id) : $id;
		if ( empty($id) ) $this->error('请选择要操作的数据!');
		$map['uid'] =   array('in',$id);
	
		switch ( strtolower($method) )
		{
			case 'forbid':
				$this->forbid('ErrorLog', $map );
				break;
			case 'resume':
				$this->resume('ErrorLog', $map );
				break;
			case 'delete':
				$this->delete('ErrorLog', $map );
				break;
			default:
				$this->error('参数非法');
		}
	}
	
	/**
	 * 错误日志详情
	 * @author 尹雷
	 */
	public function editLog()
	{
		$id = I('id', '', '/^[1-9]\d*$/');
		!$id && $this->error('参数错误');
	
		if(IS_GET) {
			//查询错误日志信息
			$log = $this->returnOne('ErrorLog', array('id' => $id, 'status' => 2));
			!$log && $this->error('错误日志信息查询失败');
		
			$log = array($log);
			int_to_string($log, array('status'=>array(1=>'已处理',-1=>'删除',0=>'禁用',2=>'未处理')));
			$this->assign('log', $log[0]);
			$this->assign('_title', '处理错误日志');
			$this->display('edit');
		}
		$data['id'] = $id;
		$data['note'] = I('note');
		$data['status'] = 1;
		$res = D('ErrorLog')->editErrorLog($data);
		if($res['error'] == 0) $this->success('编辑成功', U('ErrorLog/logList'));
		$this->error($res['info'], U('ErrorLog/logList'));
	}
	
	/**
	 * 编辑错误日志
	 * @author 尹雷
	 */
	public function logDetail()
	{
		$id = I('id', '', '/^[1-9]\d*$/');
		!$id && $this->error('参数错误');
	}
}